/**
 * 棋子移动
 */
function chessDoMove(gridX,gridY) {
    let id = DATA_CURRENT_SELECTED_CHESS;
    const cellSize = RULE_CELL_SIZE;
    // 检查是否有选中的棋子
    if (!DATA_CURRENT_SELECTED_CHESS) {
        return;
    }
    const chessObj = DATA_CHESS[id];
    const piece = chessObj.piece;
    let clan = piece.dataset.clan;
    if (clan !== DATA_CURRENT_PLAYER) {
        return;
    }
    //判断前往目标格子是否合法
    if (!isValidTargetGrid(id,gridX,gridY,"move")) {
        return;
    }
    //移动处理
    const originGrid = chessObj.grid;
    const originX = parseInt(originGrid.split(',')[0]);
    const originY = parseInt(originGrid.split(',')[1]);
    const element = document.getElementById(id);
    const currentLeft = parseFloat(element.style.left) || 0;
    const currentTop = parseFloat(element.style.top) || 0;
    element.style.left = (currentLeft+(gridX-originX)*cellSize) + 'px';
    element.style.top = (currentTop-(gridY-originY)*cellSize) + 'px';
    delete DATA_CHESS_GRID[originGrid];
    const targetGrid = gridX+','+gridY;
    chessObj.grid = targetGrid;
    DATA_CHESS[id] = chessObj;
    DATA_CHESS_GRID[targetGrid] = chessObj;
    //记录操作
    DATA_CURRENT_UNUSED_STEP--;
    let action = {
        "id":id,
        "type":"move",
        "from":originGrid,
        "to":targetGrid,
    }
    DATA_ACTION_HISTORY.push[action];
}
/**
 * 棋子吃子
 */
function chessDoCapture(rivalId,gridX,gridY) {
    // 检查是否有选中的棋子
    if (!DATA_CURRENT_SELECTED_CHESS) {
        return;
    }
    const rivalObj = DATA_CHESS[rivalId];
    const rivalPiece = rivalObj.piece;
    let rivalClan = rivalPiece.dataset.clan;
    if (rivalClan === DATA_CURRENT_PLAYER) {
        return;
    }
    //判断前往目标格子是否合法
    if (!isValidTargetGrid(DATA_CURRENT_SELECTED_CHESS,gridX,gridY,"capture")) {
        return;
    }
    //移动处理
    const cellSize = RULE_CELL_SIZE;
    const playerObj = DATA_CHESS[DATA_CURRENT_SELECTED_CHESS];
    const originGrid = playerObj.grid;
    const originX = parseInt(originGrid.split(',')[0]);
    const originY = parseInt(originGrid.split(',')[1]);
    const element = document.getElementById(DATA_CURRENT_SELECTED_CHESS);
    const currentLeft = parseFloat(element.style.left) || 0;
    const currentTop = parseFloat(element.style.top) || 0;
    element.style.left = (currentLeft+(gridX-originX)*cellSize) + 'px';
    element.style.top = (currentTop-(gridY-originY)*cellSize) + 'px';
    //对方
    const capturedGrid = gridX+','+gridY;
    let capturedObj = DATA_CHESS_GRID[capturedGrid];
    capturedObj.alive = false;
    DATA_CHESS[capturedObj.id] = capturedObj;
    delete DATA_CHESS_GRID[capturedGrid];
    DATA_CHESS_DEAD[capturedObj.id] = 0;
    document.getElementById(capturedObj.id).remove();
    //我方
    playerObj.grid = capturedGrid;
    DATA_CHESS[DATA_CURRENT_SELECTED_CHESS] = playerObj;
    delete DATA_CHESS_GRID[originGrid];
    DATA_CHESS_GRID[capturedGrid] = playerObj;
    //记录操作
    DATA_CURRENT_UNUSED_STEP--;
    let action = {
        "id":playerObj.id,
        "type":"capture",
        "from":originGrid,
        "to":capturedGrid,
    }
    DATA_ACTION_HISTORY.push[action];
    //检查是否有亡语
    checkDeathSkillTrigger(capturedObj.id,gridX,gridY);
}
/**
 * 操作是否合法
 */
function isValidTargetGrid(id,gridX,gridY,action) {
    const chessObj = DATA_CHESS[id];
    const piece = chessObj.piece;
    const config = chessObj.config;
    const type = piece.dataset.type;
    const clan = piece.dataset.clan;

    const originGrid = chessObj.grid;
    const originX = parseInt(originGrid.split(",")[0]);
    const originY = parseInt(originGrid.split(",")[1]);
    //判断是否过河
    let riverY;
    let flag_passRiver;
    let flag_targetRiver;
    let palaceZone;
    if (clan === 'red') {
        riverY = RULE_RED_RIVER;
        flag_passRiver = originY > riverY;
        flag_targetRiver = gridY > riverY;
        palaceZone = DATA_RED_PALACE;
    }
    else if (clan === 'black') {
        riverY = RULE_BLACK_RIVER;
        flag_passRiver = originY < riverY;
        flag_targetRiver = gridY < riverY;
        palaceZone = DATA_BLACK_PALACE;
    }
    else {
        riverY = RULE_BOARD_ROWS;
        flag_passRiver = false;
        flag_targetRiver = false;
    }
    //获取移动方式
    let validActions;
    if (action === 'move') {
        validActions = flag_passRiver ? config.moveAfterRiver : config.moveBeforeRiver;
    }
    else if (action === 'capture') {
        validActions = flag_passRiver ? config.captureAfterRiver : config.captureBeforeRiver;
    }
    else {
        validActions = null;
    }
    if (validActions == null || validActions.length === 0) {
        return false;
    }
    //合法的目标格子
    let isValidGrid = false;
    //判断是否走法正确
    for (let i = 0; i < validActions.length; i++) {
        let validAction = validActions[i];
        //判断是否是可以前往的目的地
        let absolute = validAction.absolute;
        let offsetX = validAction.x;
        let targetX = originX + offsetX;
        if (offsetX > 0) {//右移
            if (gridX > targetX) {
                continue;
            }
        }
        else if (offsetX < 0) {//左移
            if (gridX < targetX) {
                continue;
            }
        }
        else {
            if (gridX !== targetX) {
                continue;
            }
        }
        if (absolute && gridX !== targetX) {
            continue;
        }
        let offsetY = validAction.y;
        let targetY;
        if (clan === 'red') {
            targetY = originY + offsetY;
            if (offsetY > 0) {//上移
                if (gridY > targetY) {
                    continue;
                }
            }
            else if (offsetY < 0) {//下移
                if (gridY < targetY) {
                    continue;
                }
            }
            else {
                if (gridY !== targetY) {
                    continue;
                }
            }
        }
        else if (clan === 'black') {
            targetY = originY - offsetY;
            if (offsetY > 0) {//上移
                if (gridY < targetY) {
                    continue;
                }
            }
            else if (offsetY < 0) {//下移
                if (gridY > targetY) {
                    continue;
                }
            }
            else {
                if (gridY !== targetY) {
                    continue;
                }
            }
        }
        if (absolute && gridY !== targetY) {
            continue;
        }
        //判断是否有阻挡
        let bx = validAction.bx;
        let by = validAction.by;
        if (bx != null && by != null) {
            let blockX = bx + originX;
            let blockY;
            if (clan === 'red') {
                blockY = originY + by;
            }
            else if (clan === 'black') {
                blockY = originY - by;
            }
            let chessBlock = DATA_CHESS_GRID[blockX+','+blockY];
            if (chessBlock != null) {
                continue;
            }
        }
        isValidGrid = true;
        break;
    }
    //判断直线移动是否有阻挡
    const flag_isStraightCol = gridX - originX === 0;
    if (isValidGrid && flag_isStraightCol) {
        let flag_blocked = false;
        let blockNum = 0;
        let maxGrid = Math.max(gridY,originY);
        let minGrid = Math.min(gridY,originY);
        for (let y = minGrid + 1; y < maxGrid; y++) {
            let chessExist = DATA_CHESS_GRID[gridX+','+y];
            if (chessExist != null) {
                blockNum++;
            }
        }
        if (action === 'move') {
            if (blockNum > 0) {
                flag_blocked = true;
            }
        }
        else if (action === 'capture') {
            let captureScreen = config.captureScreen || false;
            if (captureScreen) {
                if (blockNum !== 1 ) {
                    flag_blocked = true;
                }
            }
            else {
                if (blockNum > 0) {
                    flag_blocked = true;
                }
            }
        }
        else {
            flag_blocked = true;
        }
        isValidGrid = isValidGrid && !flag_blocked;
    }
    const flag_isStraightRow = gridY - originY === 0;
    if (isValidGrid && flag_isStraightRow) {
        let flag_blocked = false;
        let blockNum = 0;
        let maxGrid = Math.max(gridX,originX);
        let minGrid = Math.min(gridX,originX);
        for (let x = minGrid + 1; x < maxGrid; x++) {
            let chessExist = DATA_CHESS_GRID[x+','+gridY];
            if (chessExist != null) {
                blockNum++;
            }
        }
        if (action === 'move') {
            if (blockNum > 0) {
                flag_blocked = true;
            }
        }
        else if (action === 'capture') {
            let captureScreen = config.captureScreen || false;
            if (captureScreen) {
                if (blockNum !== 1 ) {
                    flag_blocked = true;
                }
            }
            else {
                if (blockNum > 0) {
                    flag_blocked = true;
                }
            }
        }
        else {
            flag_blocked = true;
        }
        isValidGrid = isValidGrid && !flag_blocked;
    }
    //判断是否可以过河
    let passRiver = config.passRiver;
    if (passRiver == null) {
        passRiver = true;
    }
    if (isValidGrid && !passRiver) {
        isValidGrid = isValidGrid && !flag_targetRiver;
    }
    //判断是否可以出九宫格
    let outOfPalace = config.outOfPalace;
    if (outOfPalace == null) {
        outOfPalace = true;
    }
    if (isValidGrid && !outOfPalace) {
        let flag_outOfPalace = true;
        if (palaceZone != null && palaceZone) {
            for (let p = 0; p < palaceZone.length; p++) {
                let palace = palaceZone[p];
                if (palace[0] == gridX && palace[1] == gridY) {
                    flag_outOfPalace = false;
                    break;
                }
            }
        }
        isValidGrid = isValidGrid && !flag_outOfPalace;
    }
    //返回值
    return isValidGrid;
}